package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hjq.base.BaseActivity;
import com.hjq.base.DensityUtils;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshSceneIntentEvent;
import com.qiantu.phone.ui.adapter.SceneIntentListAdapter;
import com.qiantu.phone.utils.SceneDetailUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public final class SceneIntentListActivity extends AppActivity {

    private RecyclerView sceneIntentRecyclerView;
    private View emptySceneIntentView;
    private SceneIntentListAdapter mSceneIntentListAdapter;
    private String sceneSerialNo;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, SceneIntentListActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_intent_list;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        emptySceneIntentView = findViewById(R.id.empty_scene_intent);
        sceneIntentRecyclerView = findViewById(R.id.scene_intent_recycler_view);
        sceneSerialNo = getString("sceneSerialNo");
    }

    @Override
    protected void initData() {
        List<SceneBean> scenes = LLSceneDBManager.getInstance(this).getScenesIntentBySerialNo(sceneSerialNo);
        updateView(scenes);
    }

    private void updateView(List<SceneBean> scenes){
        if(scenes == null || scenes.size() == 0){
            getTitleBar().setRightTitle(null);
            emptySceneIntentView.setVisibility(View.VISIBLE);
            sceneIntentRecyclerView.setVisibility(View.GONE);
            setOnClickListener(R.id.btn_add);
        }else {
            getTitleBar().setRightTitle(R.string.new_create);
            emptySceneIntentView.setVisibility(View.GONE);
            sceneIntentRecyclerView.setVisibility(View.VISIBLE);
            if (mSceneIntentListAdapter == null) {
                int imageListWidth = DensityUtils.getScreenWidth(getContext()) - sceneIntentRecyclerView.getPaddingLeft() - sceneIntentRecyclerView.getPaddingRight();
                sceneIntentRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
                mSceneIntentListAdapter = new SceneIntentListAdapter(imageListWidth / 3);
                sceneIntentRecyclerView.setAdapter(mSceneIntentListAdapter);
                mSceneIntentListAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                        SceneDetailUtil.getSceneDetailAndToEdit(SceneIntentListActivity.this, SceneIntentListActivity.this, mSceneIntentListAdapter.getData().get(position).getSceneSerialNo());
                    }
                });
            }
            mSceneIntentListAdapter.setNewInstance(scenes);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshSceneIntentEvent(RefreshSceneIntentEvent event) {
        initData();
    }

    @Override
    public void onRightClick(View view) {
        toAddSceneIntent();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                toAddSceneIntent();
                break;
        }
    }

    private void toAddSceneIntent(){
        Intent intent = new Intent(this, SceneIconSelectActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("sceneType", 3);
        startActivityForResult(intent, new BaseActivity.OnActivityCallback() {
            @Override
            public void onActivityResult(int resultCode, @Nullable Intent data) {
                if (resultCode == Activity.RESULT_OK) {
                    String selectIconUrl = data.getStringExtra("DATA");
                    Intent intent = new Intent(SceneIntentListActivity.this, EditSceneIntentActivity.class);
                    intent.putExtra("bingSceneSerialNo", sceneSerialNo);
                    intent.putExtra("selectIconUrl", selectIconUrl);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}